<?php
include_once('./config/config.php');
// 接收参数id
$id = $_GET['id'];
// 通过id获取信息
$sql = "select * from admin where id = '$id'";
$admin = findOne($sql);
// var_dump($data['avatar']);

if ($_POST) {
  // 接收全部参数
  $params = $_POST;
  // var_dump($params);
  $data = [
    'username' => $params['username'],
  ];

  if ($params['oldPass']) {
    if (empty($params['oldPass'])) {
      ShowMsg("原密码不能为空");
      exit;
    }

    if ($oldPass != $admin['password']) {
      ShowMsg("原密码错误");
      exit;
    }
    

    if ($params['password'] != $params['repass']) {
      ShowMsg('密码不一致，请重试');
      exit;
    }

    // 设置密码盐
    $salt = build_ranstr();
    if (!$salt) {
      ShowMsg('密码盐缺失');
      exit;
    }

    $data['salt'] = $salt;
    $data['password'] = md5($params['password'] . $salt);
  }


  if ($_FILES['avatar'] && $_FILES['avatar']['size'] > 0) {
    // 调用上传函数
    $avatar = upload('avatar');
    if ($avatar['code'] == 0) {
      ShowMsg($avatar['msg']);
      exit;
    } else {
      $data['avatar'] = $avatar['data'];
    }
  }

  $res = update('admin', $data, "`id` = $id");
  var_dump(isset($data['avatar']));
  var_dump($_FILES['avatar']['size']);
  if ($res) {
    if (isset($data['avatar']) && $_FILES['avatar']['size'] > 0) {
      @is_file($data['avatar']) && @unlink($admin['avatar']);
    }
    ShowMsg('管理员更新成功', './adminList.php');
    exit;
  } else {
    @is_file($data['avatar']) && @unlink($data['avatar']);
    ShowMsg('管理员更新失败');
    exit;
  }
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
  <!-- 引入 -->
  <?php include_once('./common/meta.php') ?>
</head>

<style>
  .avatar_lab {
    width: 200px;
  }

  .avatar_img {
    width: 200px;
  }
</style>

<body>
  <!-- 引入头部导航 -->
  <?php include_once('./common/navbar.php') ?>
  <!-- 引入侧边菜单栏 -->
  <?php include_once('./common/sidebar.php') ?>

  <div class="content">
    <div class="header">
      <h1 class="page-title">编辑管理员</h1>
    </div>
    <ul class="breadcrumb">
      <li><a href="index.php">Home</a> <span class="divider">/</span></li>
      <li class="active">editAdmin</li>
    </ul>

    <div class="container-fluid">
      <div class="row-fluid">
        <div class="btn-toolbar">
          <button class="btn btn-primary" onClick="location='adminList.php'"><i class="icon-list"></i> 返回列表</button>
          <div class="btn-group">
          </div>
        </div>

        <div class="well">
          <div id="myTabContent" class="tab-content">
            <div class="tab-pane active in" id="home">
              <form method="POST" enctype="multipart/form-data">

                <!-- 用户名 -->
                <label>用户名</label>
                <input type="text" class="input-xxlarge" name="username" value="<?php echo $admin['username'] ?>" required>

                <!-- 密码 -->
                <label>原密码</label>
                <input type="password" class="input-xxlarge" name="oldPass">

                <label>密码</label>
                <input type="password" class="input-xxlarge" name="password">

                <!-- 确认密码 -->
                <label>确认密码</label>
                <input type="password" class="input-xxlarge" name="repass">

                <!-- 头像 -->
                <label>头像</label>
                <input type="file" name="avatar" id="avatar" hidden>
                <label for="avatar" class="avatar_lab">
                  <img src="<?php echo './static/images/upload.png' ?>" alt="" id="avatar_img">
                </label>

                <button class="btn btn-primary" type="submit" value="提交">提交</button>
              </form>
            </div>
          </div>
        </div>

        <!-- 引入底部 -->
        <?php include_once('./common/footer.php') ?>

      </div>
    </div>
  </div>

</body>

</html>
<!-- 引用公共js文件 -->
<?php include_once('./common/script.php') ?>
<script>
  // 图片预览函数
  function GetObjectURL(file) {
    // createObjectURL  给一个文件对象 可以提取出一个url本地地址出来
    var url = null;

    //判断是否为IE浏览器还是google还是其他浏览器
    if (window.createObjectURL != undefined) {
      url = window.createObjectURL(file)
    } else if (window.URL != undefined) {
      url = window.URL.createObjectURL(file)
    } else if (window.webkitURL != undefined) {
      url = window.webkitURL.createObjectURL(file)
    }

    return url
  }

  $('#avatar').change(function() {
    // 获取选中的文件
    let file = $(this)[0].files[0] ? $(this)[0].files[0] : {}
    // 获取该文件地址
    let src = GetObjectURL(file)
    // console.log(src)
    $('#avatar_img').attr('src', src)
  })
</script>